package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Member;
import cn.wolfcode.crm.domain.PointBuy;
import cn.wolfcode.crm.query.PointBuyQueryObject;
import cn.wolfcode.crm.service.IMemberService;
import cn.wolfcode.crm.service.IPointBuyService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("pointBuy")
public class PointBuyController {

    @Autowired
    private IPointBuyService pointBuyService;
    @Autowired
    private IMemberService memberService;

    @Autowired
    private IMemberService mamberService;

    @RequestMapping("")
    public String index(){
        return "pointBuy/list";
    }

    @RequestMapping("list")
    @ResponseBody
    public PageResult list(PointBuyQueryObject qo){
        PageResult result = pointBuyService.query(qo);
        int a = 1;
        return pointBuyService.query(qo);

    }

    @RequestMapping("save")
    @ResponseBody
    public JsonResult update(Long id,Long number){

        try{
            Member member = memberService.get(id);
            PointBuy pointBuy = new PointBuy();
            pointBuy.setMemberinfoCardnumber(member.getMemberNumber());
            pointBuy.setMemberinfoName(member.getMemberName());
            pointBuy.setMemberinfoGrade(member.getGrade().getName());
            pointBuy.setBuyDate(new Date());
            pointBuy.setInputUsername("111111");
        if(number>0){
            pointBuy.setInputType("增加积分");
            pointBuy.setRemark("积分充值");

        }else if(number < 0 ){
            pointBuy.setInputType("减少积分");
            pointBuy.setRemark("积分消费");
        }else{
            return  new JsonResult("请输入额度");
        }
            pointBuy.setChangePoint(number);
            pointBuyService.save(pointBuy);

        }catch(Exception e){
            e.printStackTrace();
            return new JsonResult("交易失败");
        }

        return new JsonResult();

    }


}
